package com.javenwong.android.engine;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.FontMetrics;
import android.graphics.Rect;

public final class Graphics {
	
	public static final Paint PAINT = new Paint(); 
		
	static{
		PAINT.setAntiAlias(true);
	}
	
	/**
	 * 是否点到矩形区域中
	 * @param touchX
	 * @param touchY
	 * @param x
	 * @param y
	 * @param width
	 * @param height
	 * @return
	 */
	public static final boolean isTouchInRect(float touchX, float touchY, float x, float y, float width, float height){
		
		if (touchX>=x && touchY>=y && touchX<=x+width && touchY<=y+height){
			return true;
		}
		
		return false;
	}

	/**
	 * 是否点到矩形区域中
	 * @param touchX
	 * @param touchY
	 * @param rect
	 * @return
	 */
	public static final boolean isTouchInRect(float touchX, float touchY, Rect rect){
		
		if (touchX>=rect.left && touchY>=rect.top && touchX<=rect.right && touchY<=rect.bottom){
			return true;
		}
		
		return false;
	}
	
	/**
	 * 是否点到矩形区域中
	 * @param touchX
	 * @param touchY
	 * @param sprite
	 * @return
	 */
//	public static final boolean isTouchInRect(float touchX, float touchY, Sprite sprite){
//		
//		return isTouchInRect(touchX, touchY, sprite.getX(), sprite.getY(), sprite.getWidth(), sprite.getHeight());
//	}
	
	public static final void drawRect(Canvas g, int x, int y, int width, int height){
		PAINT.setStyle(Paint.Style.STROKE);
		g.drawRect(x, y, x+width, y+height, PAINT);
	}
	
	public static final void fillRect(Canvas g, int x, int y, int width, int height){
		PAINT.setStyle(Paint.Style.FILL);
		g.drawRect(x, y, x+width, y+height, PAINT);
	}
	
	public static final int HCENTER = 1;
    public static final int VCENTER = 2;
    public static final int LEFT = 4;
    public static final int RIGHT = 8;
    public static final int TOP = 16;
    public static final int BOTTOM = 32;

    public static final int TRANS_NONE = 0;
    public static final int TRANS_MIRROR_ROT180 = 1;
    public static final int TRANS_MIRROR = 2;
    public static final int TRANS_ROT180 = 3;
    public static final int TRANS_MIRROR_ROT270 = 4;
    public static final int TRANS_ROT90 = 5;
    public static final int TRANS_ROT270 = 6;
    public static final int TRANS_MIRROR_ROT90 = 7;

    public static  void drawRegion(Canvas canvas, Paint paint, Bitmap img,
            int x_src, int y_src, int w, int h, int transform,
            int x, int y ) {
        drawRegion(canvas, img, x_src, y_src, w, h, transform, x, y, TOP|LEFT);
    }
    
    public static void drawRegion(Canvas canvas, Bitmap img,
            int x_src, int y_src, int w, int h, int transform,
            int x, int y ,int anchor) {
        canvas.save();

        switch (transform) {

        case TRANS_NONE:
            if ((anchor & HCENTER) != 0) {
                x -= (w >> 1);
            } else if ((anchor & RIGHT) != 0) {
                x -= w;
            }

            if ((anchor & VCENTER) != 0) {
                y -= (h >> 1);
            } else if ((anchor & BOTTOM) != 0) {
                y -= h;
            }
            break;

        case TRANS_ROT90:
            canvas.rotate(90, x, y);

            if ((anchor & HCENTER) != 0) {
                y -= (h >> 1);
            } else if ((anchor & LEFT) != 0) {
                y -= h;
            }

            if ((anchor & VCENTER) != 0) {
                x -= (w >> 1);
            } else if ((anchor & BOTTOM) != 0) {
                x -= w;
            }

            break;

        case TRANS_ROT180:
            canvas.rotate(180, x, y);

            if ((anchor & HCENTER) != 0) {
                x -= (w >> 1);
            } else if ((anchor & LEFT) != 0) {
                x -= w;
            }

            if ((anchor & VCENTER) != 0) {
                y -= (h >> 1);
            } else if ((anchor & TOP) != 0) {
                y -= h;
            }

            break;

        case TRANS_ROT270:
            canvas.rotate(270, x, y);

            if ((anchor & HCENTER) != 0) {
                y -= (h >> 1);
            } else if ((anchor & RIGHT) != 0) {
                y -= h;
            }

            if ((anchor & VCENTER) != 0) {
                x -= (w >> 1);
            } else if ((anchor & TOP) != 0) {
                x -= w;
            }

            break;

        case TRANS_MIRROR:
            canvas.scale(-1, 1, x, y);

            if ((anchor & HCENTER) != 0) {
                x -= (w >> 1);
            } else if ((anchor & LEFT) != 0) {
                x -= w;
            }

            if ((anchor & VCENTER) != 0) {
                y -= (h >> 1);
            } else if ((anchor & BOTTOM) != 0) {
                y -= h;
            }

            break;

        case TRANS_MIRROR_ROT90:
            canvas.scale(-1, 1, x, y);
            canvas.rotate(270, x, y);

            if ((anchor & HCENTER) != 0) {
                y -= (h >> 1);
            } else if ((anchor & LEFT) != 0) {
                y -= h;
            }

            if ((anchor & VCENTER) != 0) {
                x -= (w >> 1);
            } else if ((anchor & TOP) != 0) {
                x -= w;
            }

            break;

        case TRANS_MIRROR_ROT180:
            canvas.scale(-1, 1, x, y);
            canvas.rotate(180, x, y);

            if ((anchor & HCENTER) != 0) {
                x -= (w >> 1);
            } else if ((anchor & RIGHT) != 0) {
                x -= w;
            }

            if ((anchor & VCENTER) != 0) {
                y -= (h >> 1);
            } else if ((anchor & TOP) != 0) {
                y -= h;
            }

            break;

        case TRANS_MIRROR_ROT270:
            canvas.scale(-1, 1, x, y);
            canvas.rotate(90, x, y);

            if ((anchor & HCENTER) != 0) {
                y -= (h >> 1);
            } else if ((anchor & RIGHT) != 0) {
                y -= h;
            }

            if ((anchor & VCENTER) != 0) {
                x -= (h >> 1);
            } else if ((anchor & BOTTOM) != 0) {
                x -= h;
            }

            break;
        }
        
        PAINT.setAntiAlias(true);
        
        canvas.clipRect(x, y , x+w, y+h);
        canvas.drawBitmap(img, x-x_src , y-y_src , PAINT);
        canvas.restore();
        

    }
    
    public static void drawImage(Canvas canvas, Bitmap img, int x,
            int y) {

        drawImage(canvas, img, x, y, TRANS_NONE, LEFT | TOP);

    }

    public static void drawImage(Canvas canvas, Bitmap img, int x,
            int y, int anchor) {

        drawImage(canvas, img, x, y, TRANS_NONE, anchor);

    }
    
    public static void drawImage(Canvas canvas, Bitmap img, int x,
            int y, int transform, int anchor) {
        drawRegion(canvas, img, 0, 0, img.getWidth(), img.getHeight(), TRANS_NONE, x, y, anchor);
    }

    public static void drawText(Canvas canvas, String text, float x, float y){
    	FontMetrics fm = PAINT.getFontMetrics();
    	
    	canvas.drawText(text, x, (int)(y+(y-(y+fm.ascent))), PAINT);
    }
    
    public static void drawText(Canvas canvas, char[] text, int index, int count, float x, float y){
    	FontMetrics fm = PAINT.getFontMetrics();
    	
    	canvas.drawText(text, index, count, x, (int)(y+(y-(y+fm.ascent))), PAINT);
    }

}
